package com.dream.inspect.modules.business.service.impl;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.toolkit.StringUtils;
import com.dream.inspect.common.utils.DateFormatUtil;
import com.dream.inspect.common.utils.PageUtils;
import com.dream.inspect.common.utils.Query;
import com.dream.inspect.modules.business.dao.SuperviseRecordDao;
import com.dream.inspect.modules.business.entity.SuperviseRecordEntity;
import com.dream.inspect.modules.business.service.SuperviseRecordService;


@Service("superviseRecordService")
public class SuperviseRecordServiceImpl extends ServiceImpl<SuperviseRecordDao, SuperviseRecordEntity> implements SuperviseRecordService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Page<SuperviseRecordEntity> page = this.selectPage(
                new Query<SuperviseRecordEntity>(params).getPage(),
                new EntityWrapper<SuperviseRecordEntity>()
        );

        return new PageUtils(page);
    }

	@Override
	public List<SuperviseRecordEntity> findSuperviseRecordList(Map<String, String> params) {
		String currP = params.get("currPage");
		int currPage = 1;
		if (StringUtils.isNotEmpty(currP)) {
			currPage = Integer.valueOf(currP);
		}
		String pageS = params.get("pageSize");
		int pageSize  = 10;
		if (StringUtils.isNotEmpty(pageS)) {
			pageSize  = Integer.valueOf(pageS);
		}
    	 
    	currPage = (currPage - 1) * pageSize;
		//组装条件
    	EntityWrapper<SuperviseRecordEntity> wrapper = addCondition(params);
    	
    	List<SuperviseRecordEntity> stList = new ArrayList<SuperviseRecordEntity>();
		stList = baseMapper.findSuperviseRecordList(wrapper, currPage, pageSize);
		
		return stList;
	}
	
	@Override
	public PageUtils selectSuperviseRecordListPage(Map<String, String> params) {
		String currP = params.get("currPage");
		int currPage = 1;
		if (StringUtils.isNotEmpty(currP)) {
			currPage = Integer.valueOf(currP);
		}
		String pageS = params.get("pageSize");
		int pageSize  = 10;
		if (StringUtils.isNotEmpty(pageS)) {
			pageSize  = Integer.valueOf(pageS);
		}
		SuperviseRecordEntity srEntity = new SuperviseRecordEntity();
		
		if (StringUtils.isNotEmpty(params.get("dcdwId"))) {
			Long dcdwId = Long.valueOf(params.get("dcdwId"));
			srEntity.setDcdwId(dcdwId);
		}
		if (StringUtils.isNotEmpty(params.get("v2"))) {
			srEntity.setV2(params.get("v2"));
		}
		Page<SuperviseRecordEntity> page = new Page<>(currPage, pageSize);
		
		String findKey = params.get("findKey");
		if (StringUtils.isNotEmpty(findKey)) {
			srEntity.setV1(findKey);
		}
		String id = params.get("id");
		if (StringUtils.isNotEmpty(id)) {
			srEntity.setId(Long.valueOf(id));
		}
		List<SuperviseRecordEntity> srList = this.baseMapper.selectSuperviseRecordListPage(page, srEntity);
		NumberFormat numberFormat = NumberFormat.getInstance();
		// 设置精确到小数点后2位
		numberFormat.setMaximumFractionDigits(2);
		String result = "0";
		for (SuperviseRecordEntity sre : srList) {
			result = numberFormat.format((float) sre.getQuestionModifyNum() / (float) sre.getQuestionNum() * 100);
			sre.setPrecent(result + "%");
		}
		page.setRecords(srList);
        return new PageUtils(page);

	}

	@Override
	public int findSuperviseRecordListCount(Map<String, String> params) {

		//组装条件
		EntityWrapper<SuperviseRecordEntity> wrapper = addCondition(params);
    	//获取件数
		int counNum = baseMapper.findSuperviseRecordListCount(wrapper);
		
		return counNum;
	}
	
	@Override
	public SuperviseRecordEntity findSuperviseRecordById(Map<String, String> params) {
		//组装条件
		EntityWrapper<SuperviseRecordEntity> wrapper = addCondition(params);
		
		SuperviseRecordEntity  srEntity = new SuperviseRecordEntity();
		List<SuperviseRecordEntity> stList = new ArrayList<SuperviseRecordEntity>();
		stList = baseMapper.findSuperviseRecordList(wrapper, 0, 1);
		if (stList !=null && stList.size() > 0) {
			srEntity = stList.get(0);
		}
		return srEntity;
	}
	
	@Override
	public int checkSuperviseRecordCount(Map<String, String> params) {
		int result = 0;
		//组装条件
		EntityWrapper<SuperviseRecordEntity> wrapper = addCondition(params);
		
		result = baseMapper.checkSuperviseRecordCount(wrapper);
		
		return result;
	}
	
	@Override
	public int selectThisMonthCount(Map<String, String> params) {
		//查询条件
        EntityWrapper<SuperviseRecordEntity> wrapper = addCondition(params);

        int countNum = baseMapper.selectThisMonthCount(wrapper);
        return countNum;
	}
	
	@Override
	public SuperviseRecordEntity findSuperviseRecordForSearchPage(Map<String, String> params) {
		String id = params.get("id");
        return baseMapper.findSuperviseRecordForSearchPage(id);
	}

	@Override
	public int findSuperviseRecordForSearchPageCount(Map<String, String> params) {
		//查询条件
        EntityWrapper<SuperviseRecordEntity> wrapper = addCondition(params);

        int countNum = baseMapper.findSuperviseRecordForSearchPageCount(wrapper);
        return countNum;
	}
	
	@Override
	public SuperviseRecordEntity selectSuperviseRecordByIdDetail(Map<String, String> params) {
		SuperviseRecordEntity srEntity = new SuperviseRecordEntity();
		
		String id = params.get("id");
		if (StringUtils.isNotEmpty(id)) {
			srEntity.setId(Long.valueOf(id));
		}
		
		srEntity = this.baseMapper.selectSuperviseRecordByIdDetail(srEntity);
		
		return srEntity;
	}

	
	/**
	 * 拼接查询条件
	 * 
	 * @param params
	 * @return
	 */
	private EntityWrapper<SuperviseRecordEntity> addCondition(Map<String, String> params) {
		String id = params.get("id");
		String projectId = params.get("projectId");//专项ID
		String sortByTime = params.get("sortByTime");//时间排序
		String modifyStatus = params.get("modifyStatus");//整改状态
		String dcfs = params.get("dcfs");
		String dcdwId = params.get("dcdwId");
		String userId = params.get("userId");
		String userDwId = params.get("userDwId");
		String thisMonthFlg = params.get("thisMonthFlg");//本月标志
		String searchText = params.get("searchText");
		
		EntityWrapper<SuperviseRecordEntity> wrapper=new EntityWrapper<SuperviseRecordEntity>();
		wrapper.setEntity(new SuperviseRecordEntity());
		//督察记录ID
		if (StringUtils.isNotEmpty(id)) {
			wrapper.eq("sr.ID", id);
		}
		//专项ID
		if (StringUtils.isNotEmpty(projectId)) {
			wrapper.eq("PROJECT_ID", projectId);
		}
    	//整改状态(0：待整改，1：未整改，2：已整改)
    	if (StringUtils.isNotEmpty(modifyStatus)) {
    		wrapper.eq("MODIFY_STATUS", modifyStatus);
    	}
    	//督察方式(1：实时拍录，2：立项督察，3：分级督察)
    	if (StringUtils.isNotEmpty(dcfs)) {
    		wrapper.eq("DCFS", dcfs);
    	}
        //用户ID
        if (StringUtils.isNotEmpty(userId)) {
            wrapper.eq("sr.CREATE_USER", userId);
        }
        //用户所在单位ID(检查是否督察过的接口用)
    	if (StringUtils.isNotEmpty(userDwId)) {
    		wrapper.eq("sr.DCDW_ID", userDwId);
    	}
    	if (StringUtils.isNotEmpty(thisMonthFlg)) {
			if ("1".equals(thisMonthFlg)) {
				String dt = DateFormatUtil.getFirstDayOfMonthForString();
				wrapper.and(" Date(sr.CREATE_TIME) >= '" + dt + "'");
			}
		}
        //用户所在单位ID
    	if (StringUtils.isNotEmpty(dcdwId)) {
    		wrapper.andNew();
    		wrapper.eq("sr.DCDW_ID", dcdwId);
    		wrapper.or("sr.TO_DCDW_ID={0}", dcdwId);
    		wrapper.or("sr.GXDW_ID={0}",dcdwId);
    	}
    	
        //时间排序（1：升序，2：降序）
    	if (StringUtils.isNotEmpty(sortByTime)) {
    		if ("1".equals(sortByTime)) {
    			wrapper.orderBy("DC_TIME", true);
    		} else {
    			wrapper.orderBy("DC_TIME", false);
    		}
    	}
    	   
		//全文检索条件
		if (StringUtils.isNotEmpty(searchText)) {
			wrapper.andNew();
    		wrapper.like("sd.name", searchText);
    		wrapper.or("sr.DCMC like {0}", "%" + searchText + "%");
		}
    	
    	return wrapper;
	}

}
